
/////////////// COLLECT MONEY /////////////      
function collectMoney(collecting) {
    var amount = $.trim($('input#real_amount').val());
    if (amount.length == 0) {       
        var buttons = {};
        buttons[_('Close')] = function() {
                $(this).dialog('close');        
            };
        msg =   getMoneyAmountMessage(collecting);
        generalDialog(null, null, msg, 'warn',{
                autoOpen: true,         
                bgiframe: true,
                resizable: true,
                modal: true,                            
                buttons: buttons
            });
        return;
    }
    
    var lcInt   =   locale(decimalPoint, thousandSep, 0);
    var val     =   parseLcNum(amount, lcInt);
    if (!amount.match(/^[0-9, \,, \.]+$/) || isNaN(val) || val <= 0) {
        var buttons = {};
        buttons[_('Close')] = function() {
                $(this).dialog('close');        
            };
        generalDialog(null,null,_('Invalid amount <span class="impression">{0}</span> ',amount),'warn',{
                autoOpen: true,         
                bgiframe: true,
                resizable: true,
                modal: true,                            
                buttons: buttons
            });
        return;
    }
    styledAmount = number_format(val, 0, ',', ' ');  
    var message = getConfirmMessage(styledAmount, collecting);
    
    promptConfirm(_("Confirm"),message, function() {
        patientId   =   $('#patientIdHdn').val();
        submitPayment(patientId, val, collecting); 
        return true;
    },'YesNo','',_('Yes'));
}

function submitPayment(patientId, amount, collecting) {
    $.ajax ({   
        type:       "GET", 
        url:        "../InCashier/submitPayment?set_ajax_view",
        data:       "patientId=" + patientId +
                    "&amount=" + amount +
                    "&collecting=" + collecting,
        dataType:   "json",
        beforeSend: function() {
            showAjaxLoader('inPaymentAjaxLoaderDiv', 'inPaymentButtonDiv', true);
        },
        complete:   function() {                        
            
        },      
        success:    function(data) {
            showAjaxLoader('inPaymentAjaxLoaderDiv', 'inPaymentButtonDiv', false);
            refreshTotalPayment(data.payment);                                                                                  
        }
    }); 
        
}

function getConfirmMessage(amount, collecting) {
    if  (collecting) {
       msg  =  _('Are you sure to collect?') + 
        '<p>' + _('Amount: <span class="bigImpression">{0}</span>',amount) + '</p>';
    }
    else {
        msg = _('Are you sure to pay back?') + 
        '<p>' + _('Amount: <span class="bigImpression">{0}</span>',amount) + '</p>';
    }
    
    return msg;
}

function getMoneyAmountMessage(collecting) {
    if (collecting) {
        msg =   _("Please fill in amount.");
    }
    else {
        msg =   _("Please fill in amount to pay back.");
    }
    
    return msg;
}

function refreshTotalPayment(data) {
    $('#original_price').text(data.originalPrice);
    $('#insurance_price').text(data.insurancePrice);
    $('#patient_price').text(data.patientPrice);
    $('#advance_payment').text(data.advancePayment);
    $('#patient_debt').text(data.patientDebt);
    $('input#real_amount').val(data.absolutePatientDebt);
}
/////////////// COLLECT PAYMENTS ///////////////